from jiekou.port_job.mall.utils import sendmethod
from jiekou.port_job.mall.utils.Getkeyword import Getkeyword
from jiekou.port_job.mall.utils.sendmethod import SendMethod


class Memberlnterface:
    def __init__(self, url):
        self.url = url

    def get_auth_code(self, telephone):  # 获取验证码
        method = 'get'
        url = self.url + '/sso/getAuthCode'
        payload = {'telephone': telephone}
        return SendMethod.send_method(method=method, url=url, params=payload)

    def get_verify_code(self, telephone):  # 获取验证码值
        response = self.get_auth_code(telephone)
        print(response)
        return Getkeyword.get_keyword(response, 'data')

    def register(self, username, password, telephone):  # 注册会员登录接口
        method = 'post'
        url = self.url + '/sso/register'
        payload = {
            'username': username,
            'password': password,
            'telephone': telephone,
            'authCode': self.get_verify_code(telephone)}
        return SendMethod.send_method(method=method, url=url, data=payload)

    def login(self, payload):  # 登录会员接口
        method = 'post'
        url = self.url + '/sso/login'
        return SendMethod.send_method(method=method, url=url, data=payload)

    def get_member_token(self, payload):  # 获取token返回值
        response = self.login(payload)
        token = Getkeyword.get_keyword(response, 'token')
        return {'Authorization': f'Bearer {token}'}

    def refresh_token(self, username, password):
        method = "get"
        url = self.url + "/sso/refreshToken"
        token = self.get_member_token(payload)
        response = sendmethod.SendMethod.send_method(method=method, url=url, headers=token)
        token = Getkeyword.get_keyword(response, "token")
        return {"Authorization": f"Bearer {token}"}

    def change_password(self, payload):  # 修改密码
        method = 'post'
        url = self.url + '/sso/updatePassword'
        return sendmethod.SendMethod.send_method(method=method, url=url, data=payload)

    def get_member(self, name):  # 获取会员信息
        method = 'get'
        headers = self.get_member_token(payload)
        url = self.url + '/sso/info'
        return sendmethod.SendMethod.send_method(method=method, headers=headers, url=url, params=name)


if __name__ == '__main__':
    url = "http://47.108.206.100:8085"
    member = Memberlnterface(url)

    telephone = '13160407148'
    # print(member.get_auth_code(telephone))
    # print(member.get_verify_code(telephone))
    payload = {'username': 'Jerry5',
               'password': '123456789'}
    username = 'admin123456789'
    password = '123456'
    # print(member.register(username, password, telephone))
    print(member.refresh_token('Jerry5', '123456789'))
    # print(member.login(payload))
    # print(member.get_member(name='Jerry5'))
    print(member.get_member_token(payload))

    # change = {"password": "123456789",
    #           "telephone": telephone,
    #           "authCode": member.get_verify_code(telephone=telephone)}
    #
    # print(member.change_password(change))
